﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data;

public partial class Admin_Asp_UserControls_ShaderSelection : System.Web.UI.UserControl
{
    private List<string> m_shaders;
    private bool m_loadedLists = false;

    protected void Page_Load(object sender, EventArgs e)
    {
        LoadLists();
    }

    private void LoadLists()
    {
        if (!m_loadedLists && Session[Strings.Session_Shaders] != null)
        {
            m_shaders = Session[Strings.Session_Shaders] as List<string>;
            DataView[] f_dvs = ModelItemHandler.GetShadersLists(m_shaders);
            x_selectedShaders.DataSource = f_dvs[0];
            x_selectedShaders.DataBind();

            x_addShaders.DataSource = f_dvs[1];
            x_addShaders.DataBind();

            m_loadedLists = true;
        }
    }

    protected void CloseSelection(object sender, EventArgs e)
    {
        this.Visible = false;

        string f_id = (Request.QueryString[Strings.Query_Edit_ID] == null) ? "" : "?" + Strings.Query_Edit_ID + "=" + Request.QueryString[Strings.Query_Edit_ID];
      
        Response.Redirect(Request.Path + f_id);
    }

    /// <summary>
    /// Removes a shader from selectedShaders and then adds the removed one to the addshaders list
    /// </summary>    
  
    protected void RemoveShader(object sender, EventArgs e)
    {
        //Remove shader from x_selectedShaders
        if (x_selectedShaders.SelectedIndex != -1 && x_selectedShaders.SelectedIndex < x_selectedShaders.Items.Count)
        {
            //Add shader from x_selectedShaders
            HtmlGenericControl f_shaderType = x_selectedShaders.SelectedItem.FindControl("x_selectionShaders_shaderType") as HtmlGenericControl;

            (Session[Strings.Session_Shaders] as List<string>).Remove(f_shaderType.InnerHtml);
            m_loadedLists = false;
            LoadLists();
            //Remove it from x_addShaders

            x_selectedShadersPanel.Visible = false;
            x_selectedShaders.SelectedIndex = -1;
            //Update Session variable
            //Update the two lists
            string f_urlAppendix = (Request.QueryString[Strings.Query_Control] != null && (Request.QueryString[Strings.Query_Control] as String) == Strings.Query_Control_Shader) ? "" : "&" + Strings.Query_Control + "=" + Strings.Query_Control_Shader;

            if (Request.RawUrl.Contains('?'))
            {
                Server.Transfer(Request.RawUrl + f_urlAppendix);
            }
            else
            {
                Server.Transfer(Request.RawUrl + "?" + f_urlAppendix);
            }           
        }        
    }

    /// <summary>
    /// Adds a shader to selectedShaders and removes it from addShaders list since it's now added
    /// </summary>  
    protected void AddShader(object sender, EventArgs e)
    {

        if (x_addShaders.SelectedIndex != -1 && x_addShaders.SelectedIndex < x_addShaders.Items.Count)
        {
            //Add shader from x_selectedShaders
            HtmlGenericControl f_shaderType = x_addShaders.SelectedItem.FindControl("x_addShaders_shaderType") as HtmlGenericControl;

            (Session[Strings.Session_Shaders] as List<string>).Add(f_shaderType.InnerHtml);
            m_loadedLists = false;
            LoadLists();
            //Remove it from x_addShaders

            x_addShadersPanel.Visible = false;
            x_addShaders.SelectedIndex = -1;            
            //Update Session variable
            //Update the two lists
            string f_urlAppendix = (Request.QueryString[Strings.Query_Control] != null && (Request.QueryString[Strings.Query_Control] as String) == Strings.Query_Control_Shader) ? "" : "&" + Strings.Query_Control + "=" + Strings.Query_Control_Shader;

            if (Request.RawUrl.Contains('?'))
            {
                Server.Transfer(Request.RawUrl + f_urlAppendix);
            }
            else
            {
                Server.Transfer(Request.RawUrl + "?" + f_urlAppendix);
            }
        }
        
    }    

    protected void selectedShaders_ItemCommand(object source, DataListCommandEventArgs e)
    {
        selection_ItemCommand(source, e, x_selectedShadersPanel);        
    }

    protected void addShaders_ItemCommand(object source, DataListCommandEventArgs e)
    {
        selection_ItemCommand(source, e, x_addShadersPanel);
    }

    /// <summary>
    /// A generic itemcommand function that selects & unselects and also shows/hides a htmlControl depending on selection
    /// </summary>    
    protected void selection_ItemCommand(object source, DataListCommandEventArgs e, Control a_htmlControl)
    {
        DataList dl = source as DataList;

        int selIdx = dl.SelectedIndex;

        switch (e.CommandName.ToLower())
        {
            case "select":
                selIdx = e.Item.ItemIndex;
                a_htmlControl.Visible = true;
                break;
            case "unselect":
                selIdx = -1;
                a_htmlControl.Visible = false;
                break;
        }

        if (selIdx != dl.SelectedIndex)
            dl.SelectedIndex = selIdx;
        dl.DataBind();
    }
}